//
//  YCVideoCell.m
//  YCSchool
//
//  Created by Kylin on 16/5/1.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//  自定义collectionView的cell用于展示视频

#import "YCVideoCell.h"
#import <MediaPlayer/MediaPlayer.h>
@interface YCVideoCell()
/**bgImage*/
@property(nonatomic,weak)UIImageView *imgaeView;
@end

@implementation YCVideoCell
- (void)setBgimage:(NSString *)Bgimage{
    _Bgimage = Bgimage;
    
    [_imgaeView yc_setBlurImageWithURLString:Bgimage holderSize:self.frame.size];
}
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
    
        [self setupChildView];
    }
    
    return self;
}


- (void)setupChildView{
    
    UIImageView *imageView = [[UIImageView alloc]init];
//    imageView.userInteractionEnabled = YES;
    imageView.contentMode = UIViewContentModeScaleToFill;
    _imgaeView = imageView;
    [self.contentView addSubview:imageView];
    UIButton *playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    playBtn.userInteractionEnabled = YES;
    [playBtn addTarget:self action:@selector(playButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [playBtn setImage:[UIImage imageNamed:@"小播放"] forState:UIControlStateNormal];
    
    [self.contentView insertSubview:playBtn aboveSubview:imageView];
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
        make.height.equalTo(self.contentView);
       
    }];
    [playBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView);
        make.centerY.equalTo(self.contentView);
        make.width.height.equalTo(@31);
    }];
}

- (void)playButtonClick{
    YCLog(@"开始播放视频了");
    //创建URL
    NSURL *url = [NSURL URLWithString:@"http://v1.mukewang.com/57de8272-38a2-4cae-b734-ac55ab528aa8/L.mp4"];
    
    MPMoviePlayerViewController *moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
    
    UIViewController *viewVC = [[UIApplication sharedApplication].keyWindow.rootViewController.childViewControllers lastObject];
    //如果被modal出来了,那么下次点击控制则不再modal,防止我次modal播放器控制器
    if (viewVC.presentedViewController) {
        return;
    }
    
    //modal视频控制器
    //从下面弹上来
    [viewVC presentViewController:moviePlayerViewController animated:YES completion:^{
        
    }];
}
@end
